package cn.yangliu.nacos.management.web.sys;

import java.util.List;
import java.util.Optional;

import cn.yangliu.nacos.comm.config.security.annotations.Authorize;
import cn.yangliu.nacos.comm.config.security.enums.Logic;
import cn.yangliu.nacos.management.entity.sys.Authority;
import cn.yangliu.nacos.management.model.AuthorityInsertModel;
import cn.yangliu.nacos.management.model.AuthorityUpdateModel;
import cn.yangliu.nacos.management.service.sys.AuthorityService;
import cn.yangliu.operation.annotation.Action;
import cn.yangliu.operation.annotation.Module;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * The type of nacos.
 *
 * @author 问道于盲
 * @date 2020 -01-01
 */
@Api(value = "系统资源管理", tags = "系统资源管理")
@RestController
@RequestMapping("/sys/authority")
@Module("系统管理-资源管理")
public class AuthorityController {

    /**
     * current class instance's member.
     * The Authority service.
     */
    @Autowired
    private AuthorityService authorityService;

    /**
     * Insert authority.
     *
     * @param model the model
     * @return the authority
     */
    @PostMapping
    @ApiOperation("新增资源")
    @Authorize("authority:insert")
    @Action("新增资源")
    public Authority insert(@RequestBody @Validated AuthorityInsertModel model) {
        return authorityService.insert(model);
    }

    /**
     * Update authority.
     *
     * @param model the model
     * @return the authority
     */
    @PutMapping
    @ApiOperation("修改资源")
    @Authorize("authority:update")
    @Action("修改资源")
    public Authority update(@RequestBody @Validated AuthorityUpdateModel model) {
        return authorityService.update(model);
    }

    /**
     * Delete.
     *
     * @param id the id
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除资源")
    @Authorize("authority:delete")
    @Action("删除资源")
    public void delete(@PathVariable("id") Long id) {
        authorityService.deleteById(id);
    }

    /**
     * Find by id authority.
     *
     * @param id the id
     * @return the authority
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询资源")
    @Authorize("authority:query")
    public Authority findById(@PathVariable("id") Long id) {
        Optional<Authority> optional = authorityService.findById(id);
        return optional.orElse(null);
    }

    /**
     * Find all list.
     *
     * @return the list
     */
    @GetMapping("/findAll")
    @ApiOperation("查询所有资源")
    @Authorize(value = {"user:query", "role:query", "authority:query"}, logic = Logic.OR)
    public List<Authority> findAll() {
        return authorityService.findAll();
    }
}
